<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        window.onload=function(){
            //获取目标canvas元素对象
            var can=document.querySelector('canvas');
            //获取2d画笔
            var context=can.getContext('2d');
            //确定线条起点
            context.moveTo(100,100);
            //绘制线条到指定点
            context.lineTo(500,100);
            //绘制线条到指定点
            context.lineTo(500,500);
            //绘制线条到指定点
            context.lineTo(100,500);
            //设置线条颜色
            context.strokeStyle='pink';
            //设置线条的宽度
            context.lineWidth=20;
            //呈现线条
            context.stroke();
        }
    </script>
</head>
<body>
    <canvas style="border:1px solid red" width="600" height="600"></canvas>
</body>
</html>